package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CheckGroupService checkGroupService;

    @Override
    public void add(SetmealDTO setmealDTO) {
        Integer id = setmealDTO.getId();
        if (id!=null) {
            baseMapper.relieveBySetmealId(id);
        }
        saveOrUpdate(setmealDTO);

        id = setmealDTO.getId();
        System.out.println(id);

        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        System.out.println(checkgroupIds);
        if (checkgroupIds!=null&&checkgroupIds.length!=0) {
            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetmealAndCheckgruopIds(id,checkgroupId);

            }
        }
        if (setmealDTO.getImg()!=null) {
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmealDTO.getImg());
        }
    }


//    @Override
//    public void add(CheckGroupDTO checkGroupDTO) {
//        Integer id = checkGroupDTO.getId();
//        if (id!=null) {
//            baseMapper.relieveByGroupId(id);
//        }
//        saveOrUpdate(checkGroupDTO);
//        id = checkGroupDTO.getId();
//        System.out.println(id);
//
//        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
//        System.out.println(checkitemIds);
//        if (checkitemIds!=null&&checkitemIds.length!=0) {
//            for (Integer checkitemId : checkitemIds) {
//                baseMapper.addCheckGroupAndCheckItemIds(id,checkitemId);
//            }
//        }
//    }
//
////    @Override
////    public List<CheckItem> findAll() {
////        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper();
////        // 有效数据
////        queryWrapper.eq("is_delete",0);
////        List<CheckItem> list = list(queryWrapper);
////        return list;
////    }
////
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page = null;
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("is_delete",0);
        if (StringUtils.isEmpty(queryPageBean.getQueryString())) {
            page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        } else {
            queryWrapper.and(
                    wrapper ->
                            wrapper.like("name", queryPageBean.getQueryString())
                                    .or()
                                    .like("code",queryPageBean.getQueryString())
                                    .or()
                                    .like("helpcode",queryPageBean.getQueryString())
            );
//            queryWrapper.like("name",queryPageBean.getQueryString());
//            queryWrapper.or();
//            queryWrapper.like("code",queryPageBean.getQueryString());

            page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }
//
    @Override
    public List<Integer> findCheckGruopIdsBySetmealId(int id) {
        List<Integer> list = baseMapper.findCheckGruopIdsBySetmealId(id);
        return list;
    }

    @Override
    public SetmealVO findSetMealDetail(Integer id) {
        SetmealVO setmealVO = baseMapper.findSetmealById(id);

        List<CheckGroupVO> checkGroupVOList = baseMapper.findCheckGroupsBySetmealId(id);
        for (CheckGroupVO checkGroupVO : checkGroupVOList) {
            List<CheckItem> checkItems = checkGroupService.findCheckItemsByGid(checkGroupVO.getId());
            checkGroupVO.setCheckItemList(checkItems);
        }
        setmealVO.setCheckGroupList(checkGroupVOList);


        return setmealVO;
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = baseMapper.findSetmealById(id);
        return setmeal;
    }
//,
//    @Override
//    public List<CheckGroup> findAll() {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("is_delete",0);
//        List<CheckGroup> list = list(queryWrapper);
//        return list;
//    }
}
